menuList[1].onclick();

let trade = document.getElementById('trade');
// 控制菜单的显示与隐藏
function orderControalFun(e){
    e=e || window.event;
    var target=e.target || e.srcElement;
    let orderNav = cl("orderNav");
    let orderShow = cl("orderShow");
    if(target.getAttribute("name")=="open"){
        orderNav.style.display="none";
        orderShow.style.width="100%";
        target.setAttribute("name","close")
    }else{
        orderNav.style.display="block";
        orderShow.style.width="80%";
        target.setAttribute("name","open")
    }
}
// 点击类别筛选
let orderMenuclass = 0;
function fliterFun(e){
    e=e || window.event;
    var target=e.target || e.srcElement;
    let orderLi = document.getElementsByClassName("orderLi");
    let showOrder = cl("showOrder");
    orderLi[orderMenuclass].style.color="#000";
    target.style.color="#438Eb9";
    orderMenuclass=target.getAttribute("name");
    if(orderMenuclass==-1){
        fun(1)
    }else{

    }
    showOrder.innerHTML = target.innerHTML;
}
// 获取数据并渲染
current=1;
pageSize=10;
let flagData=1;//判断当前分页数据是那些类型 1、所有数据 2、搜索结果 
let result;
async function fun(flagOptions){
    let checkAll = document.querySelector("#checkAll");
    checkAll.checked = false;
    dels=[];
    if(flagData==1){
        result = await getData(current,pageSize);
        counts = result.data.totalCounts;
        console.log(counts);
        pages = result.data.totalPage;
        pageFun({
            current:current,
            pages:pages,
            counts:counts,
            flagOptions:flagOptions,
            pageSize:pageSize
        })
        // result = await getData(current,pageSize);
        // counts = result.data.totalCounts;
        // console.log(counts);
        // pages = result.data.totalPage;
        // pageFun({
        //     current:current,
        //     pages:pages,
        //     counts:counts
        // })
    }
}
fun(1);
console.log("123");
// 获取数据
function getData(current,pageSize){
    return new Promise((resolve, reject) => {
        axios({
            url:'/admin/trade/ManagerAllOrders',
            method:'GET',
            params:{
                page:current,
                size:pageSize
                }
            })
            .then(data => {
                console.log(data.data);
                decorateFun(data)
                resolve(data.data);
            })
            .catch(err => {
                console.log(err);
            })
    });
}

// 
function closeOrderWin(){
    let covers = cl("covers");
    let alertWindow = cl("alertWindow");
    covers.style.display ="none";
    alertWindow.style.display ="none";
}
// 批量删除
function openDeleteOrdersFun(){
    console.log(dels);
    if(dels.length==0){
        explain({
            code:3,
            msg:"请选择需要删除的订单",
            time:3000
        })
    }else{
        pointFun({
            code:6,
            msg:"是否确认删除选中的订单"
        })
    }
}
function deleteOrdersFun(){
    axios({
        url:'/admin/trade/updateAOrders',
        method:'PATCH',
        params:{
            ids:dels
        }
    })
    .then(data => {
        console.log(data);
        if(data.data.code==20000){
            closeFun();
            fun();
            explain({
                code:1,
                msg:"删除成功",
                time:3000
            })
        }
    })
    .catch(err => {
        console.log(err);
    })
}
// 删除单个订单
let userid;
function openDeleteOrderFun(e){
    e=e || window.event;
    var target=e.target || e.srcElement;
    userid=target.getAttribute('name');
    pointFun({
        code:7,
        msg:"是否删除该订单"
    })
    console.warn(target.getAttribute('name'));
}
function deleteOrderFun(){
    axios({
        url:'/admin/trade/updateAOrder',
        method:'PATCH',
        params:{
            userId:userid
        }
    })
    .then(data => {
        console.log(data);
        if(data.data.code==20000){
            closeFun();
            fun();
            explain({
                code:1,
                msg:"删除成功",
                time:3000
            })
        }
    })
    .catch(err => {
        console.log(err);
    })
}

// 搜索通过订单号
let searchInput = cl("searchInput");
function searchByUsername(){
    console.log(searchInput.value);
    console.log(current);
    console.log(pageSize);
    axios({
        url: '/admin/users/selectUsers',
        method: 'GET',
        params:{
            nickname : searchInput.value,
            page: current,
            size: pageSize
            }
        })
        .then(data => {
            if(data.data.code==20001){
                explain({
                    code:2,
                    msg:"抱歉未找到",
                    time:3000
                })
            }else{
                result = data.data.data;
                counts = result.totalCounts;
                decorateFun(data)
                pageFun({
                    current:result.page,
                    pages:result.totalPage,
                    counts:result.users.length
                })
            }
        })
        .catch(err => {
            console.log(err);
        })
}

// 渲染页面
function decorateFun(data){
    let tbodyMenu = cl("tbodyMenu");
    let dataNums = cl("dataNums");
    let pageMessage = cl("pageMessage");
    let end;
    if(current==1){
        end=data.data.data.list.length;
    }else{
        end =parseInt(current-1)*parseInt(data.data.data.size)+parseInt(data.data.data.list.length);
    }
    dataNums.innerHTML = data.data.data.list.length;
    pageMessage.innerHTML = `第${(current-1)*data.data.data.size+1}到${end}条记录，共${data.data.data.totalCounts}条`;
    tbodyMenu.innerHTML ="";
    let i=0;
//     <button class="btn handleBtn">
//     <i class=" iconfont daichulidingdan"></i>
// </button>
    for(let key of data.data.data.list){
        tbodyMenu.innerHTML += `
        <tr>
            <td><input type="checkbox" class="checkbox" name="${key.orderNo}" title="${key.img==null?'默认头像':'点击放大'}" alt="点击放大" onclick="checkboxFun()" target="${i}"  ></td>
            <td>${key.orderNo}</td>
            <td>${key.goodsName}</td>
            <td>${key.money}</td>
            <td>${key.time}</td>
            <td>${key.type}</td>
            <td>${key.count}</td>
            <td>待支付</td>
            <td>
         
            
            <a href="/admin/trade/orderMenuDetail?orderNo=${key.orderNo}">
                <button class="btn detailBtn">
                    <i class=" iconfont xiangqing-"></i>
                </button>
            </a>
            <button class="btn delBtn" onclick="openDeleteOrderFun()">
                <i class=" iconfont shanchu"></i>
            </button>
            </td>
        </tr>
            `
            i++
        }
}
// 输入框回车事件
function inputEnterFun(){
    console.log("a");
    if(event.keyCode == 13){
        searchByUsername()
    }
}

// 页面刷新
function reloadFun(){
    location.reload();
}

// 选择每页展示数量
function chooseSizeFun(){
    let selectNums = document.getElementById("selectNums");
    console.warn(selectNums.value);
    current = 1;
    pageSize =selectNums.value;
    fun();
}
// 遍历一级分类
function getSortOne(){
    let sortUlOne = cl("orderUl"); //一级容器
    sortUlOne.innerHTML =`
        <li class="orderLi" _id="-1" name="-1" flags="1" onclick="fliterFun()">全部订单</li>
    `
    axios({
        url:'/admin/goods/findFirst',
        method:'GET',
        })
        .then(data => {
            if(data.data.code==20000){
                let i=0;
                for(let key of data.data.data){
                    sortUlOne.innerHTML+=`
                    <li class="orderLi" _id="${i}" name="${key.bigId}" flags="1" onclick="fliterFun()">${key.btype}</li>
                    `
                    i++;
                }
            }
            console.log(data.data.code==20000);
            resolve(data.data.data);
        })
        .catch(err => {
            console.log(err);
        })
   
}
getSortOne()

// 通过类别查询
function getDataBySortFun(bigid){
    axios({
        url:'/admin/goods/findByBigId',
        method:'GET',
        params: {
            bigId:bigId
        }
        })
        .then(data => {
            if(data.data.code==20000){
                let i=0;
                for(let key of data.data.data){
                    sortUlOne.innerHTML+=`
                    <li class="orderLi" _id="${i}" name="${key.bigId}" flags="1" onclick="fliterFun()">${key.btype}</li>
                    `
                    i++;
                }
            }
            console.log(data.data.code==20000);
            resolve(data.data.data);
        })
        .catch(err => {
            console.log(err);
        })
}